<?php

/**
 *    swfupload批量上传控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
define('THUMB_WIDTH', 300);
define('THUMB_HEIGHT', 300);
define('THUMB_QUALITY', 85);
class AppuploadApp extends MemberbaseApp
{
	var $belong; // 上传的文件所属的模型
	var $mod_uploadedfile; //上传文件模型
	var $mod_goods_image;
	var $item_id = 0; //所属模型的ID
	var $save_path; // 储存路径
	function __construct()
	{
		$this->AppuploadApp();
	}
	function AppuploadApp()
	{
		/* 建立会话 */
		if (isset($_REQUEST["ECM_ID"]) && isset($_REQUEST['HTTP_USER_AGENT']))
		{
			$_COOKIE['ECM_ID'] = $_REQUEST["ECM_ID"];
			$_SERVER['HTTP_USER_AGENT'] = $_REQUEST['HTTP_USER_AGENT'];
		}
		else
		{
			$this->json_error('no_post_params_authorize');
			exit();
		}

		parent::__construct();

		/* 初始化 */
		if (isset($_REQUEST['belong']))
		{
			$this->belong = $_REQUEST['belong'];
		}
		else
		{
			$this->json_error('no_post_param_belong');
			exit();
		}
		if (isset($_REQUEST['item_id']))
		{
			$this->item_id = $_REQUEST['item_id'];
		}
		else
		{
			$this->json_error('no_post_param_item_id');
			exit();
		}
		switch ($this->belong)
		{
			case BELONG_ARTICLE :   $this->save_path = 'data/files/mall/article';
			break;
			case BELONG_GOODSEVAL:	$this->save_path = 'data/files/goodseval/goods_'.$this->item_id;
			break;
			case BELONG_HEADERIMG:	$this->save_path = 'data/files/headerimg/headerimg_'.$this->item_id;
			break;
			case BELONG_GOODS :   $this->save_path = 'data/files/mall/goods/goods_'.$this->item_id;
			break;
			case BELONG_LOGO:	$this->save_path = 'data/files/logo/logo_'.$this->item_id;
			break;
			case BELONG_LICENSE:	$this->save_path = 'data/files/license/license_'.$this->item_id;
			break;
			case BELONG_SCENE:	$this->save_path = 'data/files/scene/scene_'.$this->item_id;
			break;
			case BELONG_BLOG:	$this->save_path = 'data/files/blog/user_'.$this->visitor->get('user_id');
			break;
			case BELONG_MSG:	$this->save_path = 'data/files/msg/user_'.$this->visitor->get('user_id');
			break;
			default:
				$this->save_path = 'data/files/orther';
		}
		$this->mod_uploadedfile = &m('uploadedfile');
		$this->mod_goods_image = &m('goodsimage');
	}
	function index()
	{
		$this->_upload_file();
	}
	function remove(){
		$db=&db();
		$file_id = empty($_REQUEST['file_id'])?0:intval($_REQUEST['file_id']);
		if(empty($file_id)){
			$this->json_error('删除失败');
			exit();
		}
		if(empty($this->item_id)){
			$this->json_error('删除失败');
			exit();
		}
		$file = $this->mod_uploadedfile->find("file_id=$file_id and belong=$this->belong and store_id=".$this->visitor->get('user_id'));
		if(empty($file)){
			//$this->json_error('文件不存在 ');
			//exit();
		}
		
		$this->item_id=intval($this->item_id);
		if($this->belong==2){
			$sql="delete from xs_goods_image where goods_id=".$this->item_id." and file_id=".$file_id;
			$db->query($sql);
		}
		
		$this->mod_uploadedfile->drop($file_id);
		/* 返回客户端 */
		$ret_info =array(
            'file_id'   => $file_id
		);
		$this->json_result($ret_info);
	}
	function _upload_file()
	{
		$ret_info = array(); // 返回到客户端的信息
		$file = $_FILES['file'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			$this->json_error('no_upload_file');
			exit();
		}
		import('uploader.lib'); // 导入上传类
		import('image.func');
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); // 限制文件类型
		$uploader->allowed_size(2048000); // 限制单个文件大小2M
		$uploader->addFile($_FILES['file']);
		if (!$uploader->file_info())
		{
			$this->json_error($uploader->get_error());
			exit();
		}

		/* 指定保存位置的根目录 */
		$uploader->root_dir(ROOT_PATH);
		$filename  = $uploader->random_filename();
		/* 上传 */
		$file_path = $uploader->save($this->save_path, $filename);   // 保存到指定目录
		if (!$file_path)
		{
			$this->json_error('file_save_error');
			exit();
		}
		$file_type = $this->_return_mimetype($file_path);
		/* 文件入库 */
		$data = array(
            'store_id'  => $this->visitor->get('user_id'),
            'file_type' => $file_type,
            'file_size' => $file['size'],
            'file_name' => $file['name'],
            'file_path' => $file_path,
            'belong'    => $this->belong,
            'item_id'   => $this->item_id,
            'add_time'  => gmtime(),
		);
		$file_id = $this->mod_uploadedfile->add($data);
		if (!$file_id)
		{
			$this->json_error('file_add_error');
			return false;
		}

		$thumbnail = dirname($file_path) . '/'. basename($file_path).'.small.jpg';
		make_thumb(ROOT_PATH . '/' . $file_path, ROOT_PATH .'/' . $thumbnail, THUMB_WIDTH, '', THUMB_QUALITY);
		if ($this->belong == 2) // 如果是上传商品相册图片
		{
			/* 生成缩略图 */

			/* 更新商品相册 */
			$data = array(
                'goods_id'   => $this->item_id,
                'image_url'  => $file_path,
                'thumbnail'  => $thumbnail,
                'sort_order' => 255,
                'file_id'    => $file_id,
			);
			if (!$this->mod_goods_image->add($data))
			{
				$this->json_error($this->mod_goods_imaged->get_error());
				return false;
			}
		}

		/* 返回客户端 */
		$ret_info =array(
            'file_id'   => $file_id,
            'file_path' => $file_path
		);
		$this->json_result($ret_info);
	}

	function _return_mimetype($filename)
	{
		preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
		switch(strtolower($fileSuffix[1]))
		{
			case "js" :
				return "application/x-javascript";

			case "json" :
				return "application/json";

			case "jpg" :
			case "jpeg" :
			case "jpe" :
				return "image/jpeg";

			case "png" :
			case "gif" :
			case "bmp" :
			case "tiff" :
				return "image/".strtolower($fileSuffix[1]);

			case "css" :
				return "text/css";

			case "xml" :
				return "application/xml";

			case "doc" :
			case "docx" :
				return "application/msword";

			case "xls" :
			case "xlt" :
			case "xlm" :
			case "xld" :
			case "xla" :
			case "xlc" :
			case "xlw" :
			case "xll" :
				return "application/vnd.ms-excel";

			case "ppt" :
			case "pps" :
				return "application/vnd.ms-powerpoint";

			case "rtf" :
				return "application/rtf";

			case "pdf" :
				return "application/pdf";

			case "html" :
			case "htm" :
			case "php" :
				return "text/html";

			case "txt" :
				return "text/plain";

			case "mpeg" :
			case "mpg" :
			case "mpe" :
				return "video/mpeg";

			case "mp3" :
				return "audio/mpeg3";

			case "wav" :
				return "audio/wav";

			case "aiff" :
			case "aif" :
				return "audio/aiff";

			case "avi" :
				return "video/msvideo";

			case "wmv" :
				return "video/x-ms-wmv";

			case "mov" :
				return "video/quicktime";

			case "rar" :
				return "application/x-rar-compressed";

			case "zip" :
				return "application/zip";

			case "tar" :
				return "application/x-tar";

			case "swf" :
				return "application/x-shockwave-flash";

			default :
				if(function_exists("mime_content_type"))
				{
					$fileSuffix = mime_content_type($filename);
				}
				return "unknown/" . trim($fileSuffix[0], ".");
		}
	}
}

?>